<template>
  <div class="course-create-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-plus"></i> 创建课程
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称"></el-input>
        </el-form-item>
        
        <el-form-item label="年级" prop="grade">
          <el-input type="number" v-model="form.grade" placeholder="请输入年级，如：2019"></el-input>
        </el-form-item>

        <el-form-item label="上课时间" prop="time">
          <el-select v-model="courseDay" placeholder="请选择星期">
            <el-option
              v-for="(item, index) in days"
              :key="index"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
          <el-select v-model="courseTime" placeholder="请选择节次">
            <el-option
              v-for="(item, index) in times"
              :key="index"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
          <el-input-number 
            v-model="courseLength" 
            :min="1" 
            :max="4" 
            label="课程时长(节)"
          ></el-input-number>
        </el-form-item>

        <el-form-item label="上课地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入上课地点"></el-input>
        </el-form-item>

        <el-form-item label="课程学分" prop="credit">
          <el-input-number v-model="form.credit" :min="1" :max="10"></el-input-number>
        </el-form-item>

        <el-form-item label="最大容量" prop="maxSize">
          <el-input-number v-model="form.maxSize" :min="1" :max="200"></el-input-number>
        </el-form-item>

        <el-form-item label="考试地点" prop="examLocation">
          <el-input v-model="form.examLocation" placeholder="请输入考试地点"></el-input>
        </el-form-item>

        <el-form-item label="课程简介" prop="description">
          <el-input type="textarea" v-model="form.description" placeholder="请输入课程简介"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">创建课程</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/teacher/course";

export default {
  name: "TeacherCourseCreate",
  data() {
    return {
      form: {
        id:-1,
        teacherId:1,
        name: "",
        grade: 2019,
        time: "",
        location: "",
        credit: 2,
        maxSize: 50,
        examLocation: "",
        description: ""
      },
      courseDay: 0,
      courseTime: 0,
      courseLength: 2,
      days: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
      times: ["第一节", "第二节", "第三节", "第四节", "第五节", "第六节", "第七节", "第八节", "第九节"],
      rules: {
        name: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
          { min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "请选择年级", trigger: "change" }
        ],
        location: [
          { required: true, message: "请输入上课地点", trigger: "blur" }
        ],
        credit: [
          { required: true, message: "请输入学分", trigger: "blur" }
        ],
        maxSize: [
          { required: true, message: "请输入最大容量", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 组合上课时间为后端要求的格式：[1-7]-[1-9]-[1-4]
          api.create(this.form).then(() => {
            this.$message.success("课程创建成功");
            this.$router.push("/teacher/course");
          }).catch(() => {
            this.$message.error("课程创建失败");
          });
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.courseDay = 0;
      this.courseTime = 0;
      this.courseLength = 2;
    }
  }
};
</script>

<style scoped>
.course-create-wrap {
  padding: 20px;
}
.container {
  margin-top: 20px;
  background: #fff;
  padding: 20px;
  border-radius: 4px;
}
.el-select {
  margin-right: 10px;
}
</style> 